package com.smart_campus.stat.controller;

import com.smart_campus.stat.service.StatService;
import com.smart_campus.stat.util.ExportCustomerUtils;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.service.MenuService;
import com.smart_campus.sys.service.UserService;
import com.smart_campus.sys.vo.UserANDRoleANDDeptVO;
import com.smart_campus.sys.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 统计分析
 * @author Alfred
 * @date 2020/6/16 19:41
 */
@RequestMapping("stat")
@RestController
public class StatController {

    @Autowired
    private StatService statService;

    @Autowired
    private UserService userService;


    @RequestMapping("loadBrowsethestatisticalJson")
    public List<Double> loadBrowsethestatisticalJson(String year) {
        List<Double> entities = this.statService.loadBrowsethestatisticalJson(year);
        for (int i = 0; i < entities.size(); i++) {
            if (null==entities.get(i)){
                entities.set(i,0.0);
            }
        }
        return entities;

    }

    @RequestMapping("userRegistrationStat")
    public List userRegistrationStat(String year){
        List registerList=this.statService.userRegistrationStat(year);
        return registerList;
    }

    @RequestMapping("Userlocationinformation")
    public List Userlocationinformation(String year){
        List registerList=this.statService.Userlocationinformation(year);
        return registerList;
    }

    /**
     * 导出用户数据
     * @param
     * @param response
     */
    @RequestMapping("exportCustomer")
    public ResponseEntity<Object> exportCustomer(HttpServletResponse response){

        List<Map> users = userService.queryAllUserForList();

        String fileName="用户数据.xls";
        String sheetName="用户数据";
        ByteArrayOutputStream bos = ExportCustomerUtils.exportCustomer(users,sheetName);
        try {
            //处理文件名乱码
            fileName= URLEncoder.encode(fileName,"UTF-8");
            //创建 封装响应头信息的对象
            HttpHeaders headers = new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件的名称
            headers.setContentDispositionFormData("attachment",fileName);
            return new ResponseEntity<Object>(bos.toByteArray(),headers, HttpStatus.CREATED);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}




